//支付场景配置，包含接口，参数配置，描述等

import api from './service.js';

export default {
	//订单处理-单个待付款	
	'singlePayOrder': {
		//顶部描述
		payDescTop: '',

		//底部描述
		payDescBottom: '选择支付宝付款后，订单发生的金额都将使用支付宝付款，包括订单后续的任何支付环节。请您确保支付宝余额充足及状态正常，以免影响订单正常流转。',

		//支付接口
		realPayApi: () => api.updateBatchPay,

		//最终支付的账户余额金额
		formatRealBalanceAmount(item, discount) {
			const moneyCount = (item.orderAmount || item.orderAmount == 0) ? item.orderAmount : item.orderMoney;
			const realDiscountAmount = Math.min(discount.logisticsAmount || 0, discount?.coupon_price || 0);
			return Math.max((moneyCount * 1000 - realDiscountAmount * 1000) / 1000, 0);
		},

		//最终支付的支付宝金额
		formatRealAliPayAmount(item, discount) {
			const moneyCount = (item.totalAmount || item.totalAmount == 0) ? item.totalAmount : item.orderMoney;
			const realDiscountAmount = Math.min(discount.logisticsAmount || 0, discount?.coupon_price || 0);
			return Math.max((moneyCount * 1000 - realDiscountAmount * 1000) / 1000, 0);
		},

		//参数格式化
		formatParams(item, payType, password, discount) {
			const params = {
				orderIds: String(item.ids).split(','),
				orderMoney: payType == 1 ?
					discount.member_coupon_id ? (item.orderAmount * 1000 - Math.min(discount.logisticsAmount, discount.coupon_price) * 1000) / 1000 : item.orderAmount :
					discount.member_coupon_id ? (item.totalAmount * 1000 - Math.min(discount.logisticsAmount, discount.coupon_price) * 1000) / 1000 : item.totalAmount
				,
				payMethod: payType,
				password,
				expressCouponId: discount.member_coupon_id || undefined
			}
			if(payType == 10) {
				delete params.password;
			}
			return params;
		}
	},
	//订单处理-拆分订单-异常订单	
	'splitOrderPay': {
		//顶部描述
		payDescTop: '',

		//底部描述
		payDescBottom: '',

		//支付接口
		realPayApi: () => api.upDateSplitOrder,

		//最终支付的金额
		formatRealAmount(item) {
			return item.orderAmount;
		},
		//参数格式化
		formatParams(item, payType, password) {
			const params = {
				payMethod: payType,
				password: password,
				...item,
			}
			if(payType == 10) {
				delete params.password;
			}
			return params;
		}
	},
	//订单处理-修改商品-异常订单	
	'errorChangePay': {
		//顶部描述
		payDescTop: '检测到本次操作包含未支付过的订单数据，系统将自动根据订单情况分配支付方式。',

		//底部描述
		payDescBottom: '',

		//支付接口
		realPayApi: (item) => item.operaType == 'single' ? api.payFinishUpdateOrderGoods : api.payFinishBatchAddOrderGoods,

		//最终支付的金额
		formatRealAmount(item) {
			if (item.payScene == 1) {
				return item.payAmount;
			}
			if (item.payScene == 10) {
				return item.companyAliPayAmount;
			}
			if (item.payScene == -1) {
				return [item.payAmount, item.companyAliPayAmount];
			}
			return item.payAmount;
		},
		//参数格式化
		formatParams(item, payType, password) {
			const params = {
				tradeNo: item.tradeNo,
				payMethod: payType,
				password
			}
			if(payType == 10) {
				delete params.password;
			}
			return params;
		}
	},
	//订单处理-修改商品-异常订单（商城）	
	'mallErrorOrderPay': {
		//顶部描述
		payDescTop: '',

		//底部描述
		payDescBottom: '',

		//支付接口
		realPayApi: () => api.exchangeGoods,

		//最终支付的金额
		formatRealAmount(item) {
			if (item.payScene == -1) {
				return [item.balancePayAmount, item.companyAliPayAmount];
			}
			return item.payAmount;
		},
		//参数格式化
		formatParams(item, payType, password) {
			const params = {
				orderId: item.shopParams.orderId,
				orderGoodsId: item.shopParams.orderGoodsId,
				list: item.shopParams.list,
				payMethod: payType,
				password,
			}
			return params;
		}
	},
	//订单处理-一键处理商品异常
	'oneKeyErrorGoodsPay': {
		//顶部描述
		payDescTop: '',

		//底部描述
		payDescBottom: '',

		//支付接口
		realPayApi: () => api.oneKeygoodsExceptionHandle,

		//最终支付的金额
		formatRealAmount(item) {
			if (item.payScene == -1) {
				return [item.balanceTotal - item.returnBalanceTotal, item.aliPayTotal - item.returnAliPayTotal];
			}
			if (item.payScene == 10) {
				return item.aliPayTotal - item.returnAliPayTotal;
			}
			return item.balanceTotal - item.returnBalanceTotal;
		},
		//参数格式化
		formatParams(item, payType, password) {
			const params = {
				orderIds: item.orderIds,
				payMethod: payType,
				password,
				orderPayMetheds: item.orderPayMetheds
			}
			if(payType == 10) {
				delete params.password;
			}
			return params;
		}
	},

	//已售汇总-生成备货单	
	'backupOrderPay': {
		//顶部描述
		payDescTop: '',

		//底部描述
		payDescBottom: '选择支付宝付款后，当前只是金额预览，还未发生实际支付，等到真正“拿货”后才会从支付宝扣款。请您确保支付宝余额充足及状态正常，以免影响订单正常流转。',

		//支付接口
		realPayApi: () => api.payBackupOrder,

		//最终支付的金额
		formatRealBalanceAmount(item) {
			return item.realPayAmount;
		},

		//最终支付的金额
		formatRealAliPayAmount(item) {
			return item.ifChooseAlipayAmount;
		},

		//最终支付的金额
		formatRealAmount(item) {
			if (item.payScene == 1) {
				return item.realPayAmount;
			}
			if (item.payScene == 10) {
				return item.ifChooseAlipayAmount;
			}
			if (item.payScene == -1) {
				return [item.balanceAmount, item.alipayAmount];
			}
			return item.realPayAmount;
		},
		//参数格式化
		formatParams(item, payType, password) {
			const params = {
				creditAmount: item.creditAmount,
				balanceAmount: item.balanceAmount,
				alipayAmount: item.alipayAmount,
				realPayMoney: item.realPayAmount,
				stockUps: item.stockUps,
				payType,
				password,
				ignoreException: true
			}
			return params;
		}
	}
}